package communicationMessages;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Vector;
import java.util.logging.Level;

import logger.LoggerWriter;
import data.Forum;
import data.Member;
import data.MemberDataBase;
import data.SubForum;
import database.DB;

public class GetModeratorMessage extends Message {

	@Override
	public String getAnswer() {
		String[] arr = this.content.split(delimiter);
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		String ans = "";
		if (arr.length != 2)
		{
			logger.log("failed"+delimiter+"GetModerator Message wrong format"+delimiter, Level.WARNING);
			ans = "failed"+delimiter+"GetModerator Message wrong format"+delimiter;
		}
		else
		{
			//need to check that user name is administrator 
			try
				{
				String userName, subForumId ;
				userName = arr[0];
				subForumId = arr[1];
				if (DB.isAdmin(userName))
				{
					ans = "ModeratorList"+delimiter+"admin"+ delimiter;
					ResultSet res = DB.getModeratorList(subForumId);
					while (res.next())
					{
						ans+=res.getString(1) + delimiter;
					}
				}
	//			int subForumInt = Integer.valueOf(subForumId);
	//			Forum forum = Forum.getForum();
	//			SubForum subforum = forum.getSubForum(subForumInt);
	//			ans = "ModeratorList"+delimiter+MemberDataBase.getUserRoll(userName)+ delimiter;
	//			HashMap<Integer, Member> moderators = subforum.get_moderatorCollection();
	//			for (Entry<Integer, Member> moderator : moderators.entrySet()) {
	//					ans+=moderator.getValue().get_name()+delimiter;
	//			}
				}
			catch (SQLException e) {
				System.out.println("GetModeratorMessage " + e.getMessage());
			}
				
		}
			
		return ans;
	}

}
